package com.kevin.Code.Backtrack;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Vinlee Xiao
 * @Classname Permutations
 * @Description Leetcode 46. 全排列 回溯算法 中等难度 经典题目
 * @Date 2021/9/23 11:22
 * @Version 1.0
 */
public class Permutations {

    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {

        List<Integer> combine = new LinkedList<>();
        dfs(nums, combine);
        return result;
    }


    /**
     * 回溯算法
     *
     * @param nums
     * @param combine
     */
    public void dfs(int[] nums, List<Integer> combine) {


        //递归结束的条件
        if (nums.length == combine.size()) {

            result.add(new LinkedList<>(combine));
        }

        for (int i = 0; i < nums.length; i++) {

            if (combine.contains(nums[i])) {
                continue;
            }
            combine.add(nums[i]);
            dfs(nums, combine);
            combine.remove(combine.size() - 1);


        }
    }


    public static void main(String[] args) {

        int[] nums = new int[]{1, 2, 3};

        Permutations permutations = new Permutations();

        List<List<Integer>> permute = permutations.permute(nums);

        for (List<Integer> list : permute) {
            System.out.println(list.size());
        }

    }
}
